package edu.gdin.ecommerce.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import jxl.Workbook;
import jxl.write.WritableWorkbook;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Service;

import edu.gdin.ecommerce.dao.InventoryDAO;
import edu.gdin.ecommerce.model.CardBatch;
import edu.gdin.ecommerce.model.Inventory;
import edu.gdin.ecommerce.service.IInventoryService;
import edu.gdin.ecommerce.service.exception.ServiceException;
import edu.gdin.ecommerce.util.DateUtil;
import edu.gdin.ecommerce.util.ExportExcel;
import edu.gdin.ecommerce.util.FilePath;
import edu.gdin.ecommerce.util.Pager;

@Service("inventoryService")
public class InventoryService implements IInventoryService {

	private InventoryDAO inventoryDAO;

	public InventoryDAO getInventoryDAO() {
		return inventoryDAO;
	}
	@Resource
	public void setInventoryDAO(InventoryDAO inventoryDAO) {
		this.inventoryDAO = inventoryDAO;
	}
	
	
	/**
	 * 根据条件查询Inventory
	 * @param inventory
	 * @param operTime1
	 * @param operTime2
	 * @param currentPage
	 * @return
	 */
	public Pager queryInventory(Inventory inventory, String operTime1, String operTime2, int currentPage) {
		Pager pager = this.inventoryDAO.queryInventory(inventory, operTime1, operTime2, currentPage);
		return pager;
	}
	
	/**
	 *  Added By COF 
	 *  2012-05-02
	 *  根据id查询库存卡
	 */
//	@Override
	public Inventory queryInvenoryById(String id) {
		return (Inventory) inventoryDAO.findByPk(Inventory.class, id);
	}
	
	/**
	 * 根据batchNo获取Inventory
	 * @param batchNo

	 * 根据条件导出Inventory
	 * @param inventory
	 * @param operTime1
	 * @param operTime2
	 * @return
	 */
	public String exportByQuery(Inventory inventory, String operTime1, String operTime2) {
		List<Inventory> inventories = this.inventoryDAO.queryInventory(inventory, operTime1, operTime2);
		if(inventories == null || inventories.size() == 0) {
			throw new ServiceException("", "没找到符合条件的库存卡，无法导出！");
		}
		
		String sheetTitle = "库存卡信息" + DateUtil.dateFormat(DateUtil.now(), "yyyyMMddHHmmss");
		String fileName = sheetTitle + ".xls";
		String downPath = FilePath.downloadPath();
		
		File file1 = new File(downPath);
		if(!file1.isDirectory()) {
			file1.mkdir();
		}
		
		File file2 = new File(downPath + fileName);
		if(file2.exists()) {
			file2.delete();
		}
		
		// 声明Excel文件
		WritableWorkbook workBook;
		try {
			file2.createNewFile();
			workBook = Workbook.createWorkbook(file2);
			ExportExcel.inventoryByBatchNo(workBook, inventories, sheetTitle);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return FilePath.relativePath() + fileName;
	}
	
	/**
	 * 	Added by COF
	 *  Update inventory status
	 *  2012-05-04
	 */
	public boolean updateInvenory(Inventory inventory) {
		try {
			this.inventoryDAO.update(inventory);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
